"""
    create by IAmFiveHigh on 2023/8/17
"""
import time

from sorting import SelectSort, InsertSort, MergeSort, QuickSort, BubbleSort, ShellSort


class SortHelper:
    @staticmethod
    def is_sorted(sequence):
        for i in range(1, len(sequence) - 1):
            if sequence[i - 1] > sequence[i]:
                return False
        return True

    @classmethod
    def sort_test(cls, sort_name, sequence):

        start_time = time.time()
        if sort_name == "选择排序":
            SelectSort.sort(sequence)
        elif sort_name == "插入排序":
            InsertSort.sort(sequence)
        elif sort_name == "归并排序":
            MergeSort.sort(sequence)
        elif sort_name == "自底向上归并排序":
            MergeSort.sort_from_below(sequence)
        elif sort_name == "快速排序":
            QuickSort.sort(sequence)
        elif sort_name == "三路快速排序":
            QuickSort.sort_three_ways(sequence)
        elif sort_name == "冒泡排序":
            BubbleSort.sort(sequence)
        elif sort_name == "希尔排序":
            ShellSort.sort(sequence)
        end_time = time.time()

        if cls.is_sorted(sequence):
            print(f'{sort_name} n = {len(sequence)} 消耗 {end_time - start_time} 秒')
        else:
            print(f"{sort_name} 排序失败")